package com.edu.internship.busi.controller;

import ch.qos.logback.core.util.ContextUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.internship.busi.dto.UserDTO;
import com.edu.internship.busi.entity.*;
import com.edu.internship.busi.service.*;
import com.edu.internship.config.context.UserContextHolder;
import com.edu.internship.util.Tools;
import com.google.common.collect.Sets;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * [一句话描述该类的功能]
 *
 * @author : tianhuida
 * @version : [v1.0]
 * @createTime : [2022/8/2 9:49]
 */

@RestController
@RequestMapping("api/user")
@Slf4j
@Api(tags = {"用户的控制器"})
public class UserController {

    @Resource
    private IUserService userService;
    @Resource
    private IDigitCaptchaService digitCaptchaService;
    @Resource
    private IRoleService roleService;
    @Resource
    private IUserRoleService userRoleService;
    @Resource
    private IPermissionService permissionService;

    @GetMapping
    @ApiOperation(value = "分页查询-User", notes = "根据条件进行查询-User")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数", dataTypeClass = Long.class, paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页几条记录", dataTypeClass = Long.class, paramType = "query", defaultValue = "10")
    })
    public IPage<User> page(UserDTO dto, @RequestParam(defaultValue = "1") Long current,
                            @RequestParam(defaultValue = "10") Long size) {
        LambdaQueryWrapper<User> dictionaryQw = Wrappers.lambdaQuery();
        dictionaryQw.like(StringUtils.isNotBlank(dto.getName()), User::getName, dto.getName())
                .like(StringUtils.isNotBlank(dto.getPhone()), User::getPhone, dto.getPhone())
                .orderByAsc(User::getCreateTime);
        IPage<User> usList = userService.page(new Page<>(current, size), dictionaryQw);
//        List<User> usList = userService.list(dictionaryQw);
        for (int i = 0; i < usList.getRecords().size(); i++) {
            LambdaQueryWrapper<UserRole> urQw = Wrappers.lambdaQuery();
            urQw.eq(UserRole::getUId,usList.getRecords().get(i).getId());
            List<UserRole> urList = userRoleService.list(urQw);
            if (CollectionUtils.isNotEmpty(urList)) {
                LambdaQueryWrapper<Role> rpQw = Wrappers.lambdaQuery();
                List<String> rList = urList.stream().map(UserRole::getRId).collect(Collectors.toList());

                rpQw.in(Role::getId,rList);
                List<Role> rpList = roleService.list(rpQw);

                Set<Role> roleSet = Sets.newHashSet(rpList);

                usList.getRecords().get(i).setRoleSet(roleSet);
            }

        }

        log.info("数据:{}",usList);


//        return userService.page(new Page<>(current, size), usList);
        return usList;
    }

    @PostMapping("/create")
    @ApiOperation(value = "新增-User",notes = "新增-User")
    public void create(@Validated @RequestBody UserDTO dto) {
        userService.createUser(dto);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新-User",notes = "更新-User")
    public void update(@Validated @RequestBody UserDTO dto) {
        userService.updateUser(dto);
    }
    
    @PostMapping("/remove")
    @ApiOperation(value = "删除-User", notes = "删除-User")
    public void remove(@RequestBody List<String> id) {
        userService.removeUser(id);
    }

    @GetMapping("/getById")
    @ApiOperation(value = "获取信息id-User",notes = "获取信息id-User")
    public List<User> getById(UserDTO dto) {
        LambdaQueryWrapper<User> userQw = Wrappers.lambdaQuery();
        userQw.eq(User::getId, dto.getId());
        return userService.list(userQw);
    }

    @GetMapping("/getByUsername")
    @ApiOperation(value = "获取信息username-User",notes = "获取信息username-User")
    public List<User> getByUsername(UserDTO dto) {
        LambdaQueryWrapper<User> userQw = Wrappers.lambdaQuery();
        userQw.eq(User::getUsername, dto.getUsername());
        return userService.list(userQw);
    }

    @PostMapping("/test")
    @ApiOperation(value = "接口测试专用", notes = "接口测试专用")
    public boolean validate(String captchaKey, String captchaCode) {

        if (digitCaptchaService.validate(captchaKey,captchaCode)) {
            System.out.println("校验成功");
        } else {
            System.out.println("校验失败");
        }
        return true;
    }


}
